﻿using DH.Core.MapperHelper;
using DH.Data;
using DH.Data.ORM.EF;
using DH.Data.ORM.EF.Extensions;
using DH.Entity;
using DH.Service.ViewModels;
using DH.Service.ViewModels.Dto;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DH.Service.Domain
{
   public class UiFeaturesButtonService: BaseService<UiFeaturesButtonEntity, string>
    {
        protected IServiceCollection _serviceCollection;

        public UiFeaturesButtonService(IServiceCollection service, DbCoreUnitOfWork unitOfWork) : base(service,unitOfWork)
        {
            this._serviceCollection = service;
        }

        public UiFeaturesButtonEntity Add(UiFeaturesButtonViewModel viewModel)
        {
            UiFeaturesButtonEntity model;
            model = DHMapper.Map<UiFeaturesButtonViewModel, UiFeaturesButtonEntity>(viewModel);
            model.CreatedOn = DateTimeOffset.Now;
            model.UpdatedOn = DateTimeOffset.Now;
            model.SortIndex = 0;

            InsertSync(model);

            return model;
        }
        public bool Add(List<UiFeaturesButtonViewModel> viewModels) {

            //删除旧数据
            if (viewModels.Any())
            {
                var uiViewId = viewModels.First().UiViewId;
                DeleteUiFeatureButtonFuncs(uiViewId);
            }

            var entitys = new List<UiFeaturesButtonEntity>();
            foreach (var vm in viewModels)
            {
                UiFeaturesButtonEntity model;
                model = DHMapper.Map<UiFeaturesButtonViewModel, UiFeaturesButtonEntity>(vm);
                model.CreatedOn = DateTimeOffset.Now;
                model.UpdatedOn = DateTimeOffset.Now;
                //model.SortIndex = 0;

                entitys.Add(model);
            }

            _unitOfWork.ExecuteAndCommit(() => {
                _repository.Add(entitys);
            });

            return true;

        }

        public  List<UiFeaturesButtonEntity> GetList(string uiViewId)
        {
            var res = GetByQuery(c => c.UiViewId == uiViewId);
            return res.ToList();
        }

        /// <summary>
        /// 删除视图下的按钮列表
        /// </summary>
        /// <param name="uiViewId"></param>
        /// <returns></returns>
        public bool DeleteUiFeatureButtonFuncs(string uiViewId)
        {
            var uiFeatureButtons = GetByQuery(c => c.UiViewId == uiViewId);
            _unitOfWork.ExecuteAndCommit(() =>
            {
                _repository.Delete(uiFeatureButtons);
            });
            return true;
        }
        /// <summary>
        /// 获取status 为true 的列表
        /// </summary>
        /// <param name="uiViewId"></param>
        /// <returns></returns>
        public List<UiFeaturesButtonEntity> GetValidList(string uiViewId)
        {
            var res = GetByQuery(c => c.UiViewId == uiViewId&&c.Status==true);
            return res.ToList();
        }

        public List<ViewButtonsDto> GetViewButtons(string uiViewId)
        {
            var sql = string.Format(@"SELECT uifb.UiViewId,
                                            uifb.ButtonId,
                                            sb.Code AS ButtonCode,
                                            sb.Name AS ButtonName,
                                            sb.Icon AS ButtonIcon,
                                            uifb.FeatureId,
                                            uifb.SortIndex
                                                FROM  UiFeaturesButton uifb
                                                JOIN Sys_Button sb ON uifb.ButtonId=sb.Id
                                                WHERE uifb.Status=1
                                                AND sb.Status=1
                                                AND uifb.UiViewId='{0}'
                                                ORDER BY uifb.SortIndex
                                    ", uiViewId);
            var res = this._repository.Context.Database.SqlQuery<ViewButtonsDto>(sql);
            return res.ToList();

        }
    }
}
